package com.itheima;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

//@WebServlet("/Servlet02")
public class Servlet02 extends HttpServlet {
    private  int i=0;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = getServletContext();
        resp.setCharacterEncoding("gbk");
        Object key = servletContext.getAttribute("key");
        String s = (String)servletContext.getAttribute("s");
        if (s!=null){
            i=Integer.parseInt(s);
        }
        if (i>=5){
            PrintWriter writer = resp.getWriter();
            writer.print("禁止恶意攻击，登录失效");
        }else {
            if (key == null) {
                PrintWriter writer = resp.getWriter();
                writer.print("用户未登录，请进行登录");
                i++;
                servletContext.setAttribute("s", i + "");
            } else {
                PrintWriter writer = resp.getWriter();
                writer.print("您已进入主页");
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
